<?php

namespace app\admin\model;

use think\Model;

class ArticleManage extends Model
{
    // 表名
    protected $name = 'article_manage';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'publishstatus_text',
        'set_topstatus_text',
        'recommendstatus_text',
        'commentstatus_text',
        'deletestatus_text',
        'published_time_text',
        'delete_time_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getPublishstatusList()
    {
        return ['0' => __('Publishstatus 0'),'1' => __('Publishstatus 1')];
    }     

    public function getSetTopstatusList()
    {
        return ['0' => __('Set_topstatus 0'),'1' => __('Set_topstatus 1')];
    }     

    public function getRecommendstatusList()
    {
        return ['0' => __('Recommendstatus 0'),'1' => __('Recommendstatus 1')];
    }     

    public function getCommentstatusList()
    {
        return ['1' => __('Commentstatus 1'),'0' => __('Commentstatus 0')];
    }     

    public function getDeletestatusList()
    {
        return ['0' => __('Deletestatus 0'),'1' => __('Deletestatus 1')];
    }     

    public function getStatusList()
    {
        return ['normal' => __('Normal'),'hidden' => __('Hidden')];
    }     


    public function getPublishstatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['publishstatus'];
        $list = $this->getPublishstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSetTopstatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['set_topstatus'];
        $list = $this->getSetTopstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRecommendstatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['recommendstatus'];
        $list = $this->getRecommendstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCommentstatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['commentstatus'];
        $list = $this->getCommentstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDeletestatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['deletestatus'];
        $list = $this->getDeletestatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPublishedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['published_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['delete_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPublishedTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function articlecategory()
    {
        return $this->belongsTo('ArticleCategory', 'articlecategory_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
